export function getDateDiff(dateTimeStamp) {
	let result
	let minute = 1000 * 60
	let hour = minute * 60
	let day = hour * 24
	let halfamonth = day * 15
	let month = day * 30
	let now = new Date().getTime()
	let diffValue = now - dateTimeStamp
	if (diffValue < 0) {
		return
	}
	let monthC = diffValue / month
	let weekC = diffValue / (7 * day)
	let dayC = diffValue / day
	let hourC = diffValue / hour
	let minC = diffValue / minute
	if (monthC >= 1) {
		if (monthC <= 12)
			result = "" + parseInt(monthC) + "月前"
		else {
			result = "" + parseInt(monthC / 12) + "年前"
		}
	} else if (weekC >= 1) {
		result = "" + parseInt(weekC) + "周前"
	} else if (dayC >= 1) {
		result = "" + parseInt(dayC) + "天前"
	} else if (hourC >= 1) {
		result = "" + parseInt(hourC) + "小时前"
	} else if (minC >= 1) {
		result = "" + parseInt(minC) + "分钟前"
	} else {
		result = "刚刚"
	}
	return result
}
export function getFormatTime(number, format) {
	let formateArr = ['Y', 'M', 'D', 'h', 'm', 's']
	let returnArr = []
	let date = new Date(number * 1000)
	returnArr.push(date.getFullYear())
	returnArr.push(formatNumber(date.getMonth() + 1))
	returnArr.push(formatNumber(date.getDate()))
	returnArr.push(formatNumber(date.getHours()))
	returnArr.push(formatNumber(date.getMinutes()))
	returnArr.push(formatNumber(date.getSeconds()))
	for (let i in returnArr) {
		format = format.replace(formateArr[i], returnArr[i])
	}
	return format
}
function formatNumber(n) {
	n = n.toString()
	return n[1] ? n : '0' + n
}
// 判断变量是对象还是数组
function checkArrayOrObject(data, type) {
	if(type == 'Array') {
		if(data instanceof Array) {
			return true
		} else {
			return false
		}
	} else if(type == 'Object') {
		if(data instanceof Array) {
			return false
		} else if(data instanceof Object) {
			return true
		}
		else {
			return false
		}
	}
}

export function modStorageSync(key, data) {
	// console.log('dataIsObject:', checkArrayOrObject(data, 'Object'))
	// 通过key获取到缓存的对象，把data对像加入或是修改
	let value
	try {
		value = uni.getStorageSync(key)
	} catch(e) {
		return e
	}
	if(checkArrayOrObject(data, 'Object') && checkArrayOrObject(value, 'Object')){
		try{
			uni.setStorageSync(key,Object.assign(value, data))
		} catch(e) {
			return e
		}
	}
	return 0
	
}
export function moneyFormat(money, digit) {
     digit = digit > 0 && digit <= 20 ? digit : 2;
     money = parseFloat(money).toFixed(digit);
     let integerArr = money.split(".")[0].split("").reverse();
     let decimals = money.split(".")[1];
     let tempArr = "";
     for (let i=0,k=integerArr.length;i<k;i++) {
             let cammaTag = (i+1)%3==0 && (i+1)!=k ? "," : "";
         tempArr += integerArr[i] + cammaTag;
     }
     money = tempArr.split("").reverse().join("") + "." + decimals;
     return money;
}